<?php
namespace app\admin\controller;
use app\admin\logic\WorkStationScanTotalLogic;

/**
 * 工位扫码统计api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2020/04/20
 * Time: 16:33
 */
class WorkStationScanTotal extends Base {

    public function __construct(){
        parent::__construct();
        $this->wsst_logic = new WorkStationScanTotalLogic();
    }

    /**
     * 获取工位扫码统计
     * @return \think\response\Json
     */
    public function ajax_get_wsst(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->wsst_logic->get_search_wsst_maps();
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('visitor_record')->get_list($maps,'id,COUNT(id) AS vr_num,mini_page,mini_params','vr_num desc','mini_params',$limit,$params['rows']);
        foreach($list as $k => $v){
            //获取工位
            $ws_maps = [];
            $ws_maps[] = ['id','=',$v['mini_params']];
            $ws_detail = model('work_station')->get_detail($ws_maps,'id,regionid,number,company_name');
            $list[$k]['number'] = !empty($ws_detail) ? $ws_detail['number'] : '';
            $list[$k]['company_name'] = !empty($ws_detail) ? $ws_detail['company_name'] : '';

            $r_maps = [];
            $r_maps[] = ['id','=',$ws_detail['regionid']];
            $r_detail = model('region')->get_detail($r_maps,'id,floorid');

            //获取楼层
            $f_maps = [];
            $f_maps[] = ['id','=',$r_detail['floorid']];
            $f_detail = model('floor')->get_detail($f_maps,'id,branchid,name');
            $list[$k]['floor_zw'] = !empty($f_detail) ? $f_detail['name'] : '';

            //获取分部
            $b_maps = [];
            $b_maps[] = ['id','=',$f_detail['branchid']];
            $b_detail = model('branch')->get_detail($b_maps,'id,name');
            $list[$k]['branch_zw'] = !empty($b_detail) ? $b_detail['name'] : '';
        }

        $total_num = model('visitor_record')->get_count($maps,'id','mini_params');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->wsst_logic->get_response(1,'OK',$data));
    }
}